//-----------------------------------------------------------------------
// <copyright file="AdjustQuantityDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public abstract class AdjustQuantityDAOBase
    {
        #region Common methods
        public virtual AdjustQuantity CreateAdjustQuantityFromReader(IDataReader reader)
        {
            AdjustQuantity item = new AdjustQuantity();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("AdjustQuantityID"))) item.AdjustQuantityID = (int)reader["AdjustQuantityID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Amount"))) item.Amount = (int)reader["Amount"];
                if (!reader.IsDBNull(reader.GetOrdinal("AdjustedDate"))) item.AdjustedDate = (DateTime)reader["AdjustedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("Comment"))) item.Comment = (string)reader["Comment"];
                if (!reader.IsDBNull(reader.GetOrdinal("BulkOrderID"))) item.BulkOrderID = (int)reader["BulkOrderID"];
                if (!reader.IsDBNull(reader.GetOrdinal("AdjustedByUserID"))) item.AdjustedByUserID = (int)reader["AdjustedByUserID"];
                if (!reader.IsDBNull(reader.GetOrdinal("POID"))) item.POID = (int)reader["POID"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateAdjustQuantityFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateAdjustQuantity methods
            
        public virtual void CreateAdjustQuantity(AdjustQuantity adjustQuantity)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spAdjustQuantityInsert");
                
                dbCommandWrapper.AddInParameter("@Amount", DbType.Int32, adjustQuantity.Amount);
                dbCommandWrapper.AddInParameter("@AdjustedDate", DbType.DateTime, adjustQuantity.AdjustedDate);
                dbCommandWrapper.AddInParameter("@Comment", DbType.String, adjustQuantity.Comment);
                dbCommandWrapper.AddInParameter("@BulkOrderID", DbType.Int32, adjustQuantity.BulkOrderID);
                dbCommandWrapper.AddInParameter("@AdjustedByUserID", DbType.Int32, adjustQuantity.AdjustedByUserID);
                dbCommandWrapper.AddInParameter("@POID", DbType.Int32, adjustQuantity.POID);
                dbCommandWrapper.AddOutParameter("@AdjustQuantityID", DbType.Int32, 0);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
                adjustQuantity.AdjustQuantityID = (int)dbCommandWrapper.GetParameterValue("@AdjustQuantityID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateAdjustQuantityException, ex);
            }
        }

        #endregion

        #region UpdateAdjustQuantity methods
        
        public virtual void UpdateAdjustQuantity(AdjustQuantity adjustQuantity)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spAdjustQuantityUpdate");            
                
                dbCommandWrapper.AddInParameter("@AdjustQuantityID", DbType.Int32, adjustQuantity.AdjustQuantityID);
                dbCommandWrapper.AddInParameter("@Amount", DbType.Int32, adjustQuantity.Amount);
                dbCommandWrapper.AddInParameter("@AdjustedDate", DbType.DateTime, adjustQuantity.AdjustedDate);
                dbCommandWrapper.AddInParameter("@Comment", DbType.String, adjustQuantity.Comment);
                dbCommandWrapper.AddInParameter("@BulkOrderID", DbType.Int32, adjustQuantity.BulkOrderID);
                dbCommandWrapper.AddInParameter("@AdjustedByUserID", DbType.Int32, adjustQuantity.AdjustedByUserID);
                dbCommandWrapper.AddInParameter("@POID", DbType.Int32, adjustQuantity.POID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateAdjustQuantityException, ex);
            }
        }
        
        #endregion

        #region DeleteAdjustQuantity methods
        public virtual void DeleteAdjustQuantity(int adjustQuantityID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spAdjustQuantityDelete");
                
                dbCommandWrapper.AddInParameter("@AdjustQuantityID", DbType.Int32, adjustQuantityID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteAdjustQuantityException, ex);
            }
        }

        #endregion

        #region GetAdjustQuantity methods
        
        public virtual AdjustQuantity GetAdjustQuantity(int adjustQuantityID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spAdjustQuantityGet");
                
                dbCommandWrapper.AddInParameter("@AdjustQuantityID", DbType.Int32, adjustQuantityID);
                
                AdjustQuantity adjustQuantity =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    if (reader.Read())
                    {
                        adjustQuantity = CreateAdjustQuantityFromReader(reader);
                        reader.Close();
                    }
                }
                return adjustQuantity;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetAdjustQuantityException, ex);
            }
        }
        
        #endregion

        #region GetAdjustQuantities methods
        public virtual AdjustQuantityCollection GetAdjustQuantities(AdjustQuantityColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spAdjustQuantityGetList");
                
                dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
                dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
                dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
                dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
                AdjustQuantityCollection adjustQuantityCollection = new AdjustQuantityCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    while (reader.Read())
                    {
                        AdjustQuantity adjustQuantity = CreateAdjustQuantityFromReader(reader);
                        adjustQuantityCollection.Add(adjustQuantity);
                    }
                    reader.Close();
                }
                totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
                return adjustQuantityCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetAdjustQuantitiesException, ex);
            }
        }
        
        public virtual AdjustQuantityCollection GetAdjustQuantities(AdjustQuantityColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetAdjustQuantities(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}